$(function(){
	opinionPretty();
	accessoryPretty();
	initChoosingMulti();
	initMultiPoint();
	initReceiverChoice();
	initModifySelection();
	initChooseOne();
	initRemoveOne();
	initChooseOK();
	initIsAgreeOrNot();
	initOperationBar();
	initNextReceiverChoice();
	initNextChooseOK();
});

/* -- 公共函数部分 -- */
// 刷新“操作”按钮
function refreshOprations()
{
	var is_multi_flag = $("#is_multi_flag").val();
	var ispartner = $("#ispartner").val();
	var opt_1 = $("#opt_1");
	var opt_2 = $("#opt_2");
	// 如果“同意”选中的话
	if ($("#examin_opinion_1:checked").size() == 1)
	{
		switch(is_multi_flag)
		{
			case "0":
				opt_1.removeAttr("disabled");
				opt_2.removeAttr("disabled");
				break;
			case "1":
				switch (ispartner)
				{
					case "0":
						opt_1.removeAttr("disabled");
						opt_2.removeAttr("disabled");
						break;
					case "1":
						opt_1.removeAttr("disabled");
						opt_2.attr("disabled", "disabled");
				}
		}
	}
	// 如果“不同意”选中的话
	if ($("#examin_opinion_2:checked").size() == 1)
	{
		switch(is_multi_flag)
		{
			case "0":
				opt_1.attr("disabled", "disabled");
				opt_2.removeAttr("disabled");
				break;
			case "1":
				// alert("yes!");
				switch (ispartner)
				{
					case "0":
						opt_1.removeAttr("disabled");
						opt_2.removeAttr("disabled");
						break;
					case "1":
						opt_1.removeAttr("disabled");
						opt_2.attr("disabled", "disabled");
				}
		}
	}
}

// 显示或关闭 modal 时，隐藏和显示表单
function toggleInput(flag)
{
	switch (flag)
	{
		case true:
			return $("#operation_bar input, #doc_title_panel input, #doc_main_panel input, #doc_main_panel select")
						.not("[@type='hidden']")
						.css("visibility", "hidden");
			break;
		case false:
			return $("#operation_bar input, #doc_title_panel input, #doc_main_panel input, #doc_main_panel select")
						.not("[@type='hidden']")
						.css("visibility", "visible");
	}
}

// 检查审查意见，看是否满足条件，如果满足，那么返回 true，如果不满足，那么返回 false
function checkOpinion()
{
	if ($("#examin_opinion_2:checked").size() == 1 && $.trim($("#input_opinion").val()) == "")
	{
		window.alert("您选择了“不同意”，请在“意见内容”处填写理由！");
		$("#input_opinion").focus().select();
		return false;
	}
	return true;
}

// 提交数据
function postData()
{
	var url = "wf_examin2.php";
	var pars = {
		"page_status" : 1,
		"doc_id" : $("#doc_id").val(),
		"user_id": $("#user_id").val(),
		"is_multi_flag": $("#is_multi_flag").val(),
		"doc_mulpnt": $("#doc_mulpnt").val(),
		"doc_receiver_list": $("#doc_receiver_list").val(),
		"doc_next_receiver": $("#doc_next_receiver").val(),
		"ispartner": $("#ispartner").val(),
		"goon_or_finish": $("#goon_or_finish").val(),
		"exam_opinion": $("#exam_opinion").val(),
		"input_opinion": $("#input_opinion").val()
	};
	$.post(url, pars, function(text){
		if (/^Success/g.test(text))
		{
			window.alert("数据提交成功！");
			window.close();
		}
		else
		{
			window.alert(text);
		}
	});
}
/* ------------------ */

// 初始化意见输入框
function opinionPretty()
{
	$("#input_opinion").addClass("opinion_text_style")
		.focus(function(){
			$(this).removeClass("opinion_text_style");
			this.select();
		})
		.blur(function(){
			if ($.trim($(this).val()) == "") { $(this).addClass("opinion_text_style"); }
		});
}

// 初始化附件栏
function accessoryPretty()
{
	$("#doc_accessory").find("a").each(function(){
		var filestr;
		filestr = $(this).html().split("/");
		$(this).html(filestr[filestr.length - 1]);
	});
}

// 初始化“单人”或“多人”审批版块
function initChoosingMulti()
{
	var panelblock = $(".doc_main_right_child_panel");
	var is_multi_flag = $("#is_multi_flag");
	panelblock.hide();
	$("input[@name='radio_ismulti[]']").each(function(i){
		if (i == 0)
		{
			$(this).click(function(){
				panelblock.hide();
				is_multi_flag.val(0);
				refreshOprations();
			});
		}
		else
		{
			$(this).click(function(){
				if ($("#doc_main_right_select_2 option").size() != 0)
				{
					is_multi_flag.val(1);
					panelblock.show();
					refreshOprations();
				}
				else
				{
					$("#multi_people_choosing").modal({
						onShow: function(){ toggleInput(true);	},
						onClose: function(){
							$("input[@name='radio_ismulti[]']:first").attr("checked", "checked");
							is_multi_flag.val(0);
							toggleInput(false);
							$.modal.close();
							refreshOprations();
						}
					});
				}
			});
		}
	});
}

// 初始化选择通过人数
function initMultiPoint()
{
	$("#doc_main_right_select_1").change(function(){
		var sv = $(this).val();
		$("#doc_mulpnt").val(sv);
	});
}

// 初始化选择收件人
function initReceiverChoice()
{
	getCompanyList("#choose_company", "../xmls/company_position.php", -1);
	$("#choose_company").change(function(){
		getSectionList("#choose_section", "../xmls/company_position.php", $(this).val(), -1);
		$("#choose_people").html("");
	});
	$("#choose_section").change(function(){
		getUserList("#choose_people", "../xmls/users.php", "s", $(this).val(), -1, $("#user_id").val());
	});
}

// 初始化修改多人审批
function initModifySelection()
{
	$("#modify_selection").click(function(){
		$("#multi_people_choosing").modal({
			onShow: function(){
				$("#chosen_people").html($("#doc_main_right_select_2").html());
				toggleInput(true);
			},
			onClose: function(){
				toggleInput(false);
				$.modal.close();
			}
		});
	});
}

// 初始化选择收件人处的“->”按钮
function initChooseOne()
{
	$("#choose_one").click(function(){
		var selected = $("#choose_people").find("option:selected");
		var sv = selected.attr("value");
		if ($("#chosen_people option[@value='" + sv + "']").size() == 0 && sv != "-1")
		{
			$("<option></option>")
				.attr("value", sv)
				.html(selected.html())
				.appendTo("#chosen_people");
		}
	});
}

// 初始化选择收件人处的“<-”按钮
function initRemoveOne()
{
	$("#remove_one").click(function(){
		$("#chosen_people").find("option:selected").remove();
	});
}

// 初始化选择收件人处的“确定”按钮
function initChooseOK()
{
	$("#choose_ok").click(function(){
		if ($("#chosen_people option").size() == 0)
		{
			$("#chosen_people_error").fadeIn("slow").fadeOut(8000);
		}
		else
		{
			var panelblock = $(".doc_main_right_child_panel");
			var v_array = [];
			$("#is_multi_flag").val(1);
			
			$("#chosen_people option").each(function(i){
				v_array.push($(this).attr("value"));
			});
			$("#doc_receiver_list").val(v_array.join(","));
			
			panelblock.show();
			$("#doc_main_right_select_2").html($("#chosen_people").html());
			toggleInput(false);
			$.modal.close();
			refreshOprations();
		}
	});
}

// 点击“同意”或“不同意”时所发生的事件
function initIsAgreeOrNot()
{
	$("#examin_opinion_1").click(function(){
		$("#exam_opinion").val($(this).val());
		refreshOprations();
	});
	$("#examin_opinion_2").click(function(){
		$("#exam_opinion").val($(this).val());
		refreshOprations();
		$("#input_opinion").focus().select();
	});
}

// 初始化操作栏
function initOperationBar()
{
	// 点击“继续流转”按钮触发的事件
	$("#opt_1").click(function(){
		if (checkOpinion())
		{
			$("#goon_or_finish").val(1);
			if ($("#is_multi_flag").val() == 1 && $("#ispartner").val() == 1)
			{
				postData();
			}
			else
			{
				$("#next_people_choosing").modal({
					onShow: function() { toggleInput(true); },
					onClose: function() { toggleInput(false); $.modal.close(); }
				});
			}
		}
	});
	// 点击“结束审批”按钮触发的事件
	$("#opt_2").click(function(){
		if (checkOpinion())
		{
			$("#goon_or_finish").val(9);
			postData();
		}
	});
	
	$("#opt_3").click(function(){
		if (confirm("真的要退出吗？")) { window.close(); }
	});
}

// 初始化下一个收件人选择版块
function initNextReceiverChoice()
{
	getCompanyList("#next_choose_company", "../xmls/company_position.php", -1);
	$("#next_choose_company").change(function(){
		getSectionList("#next_choose_section", "../xmls/company_position.php", $(this).val(), -1);
		$("#next_choose_people").html("");
	});
	$("#next_choose_section").change(function(){
		getUserList("#next_choose_people", "../xmls/users.php", "s", $(this).val(), -1);
	});
}

// 初始化下一位收件人“确定”按钮事件
function initNextChooseOK()
{
	$("#next_choose_ok").click(function(){
		var choose_people = $("#next_choose_people");
		if (choose_people.find("option").size() == 0 || choose_people.val() == "-1")
		{
			$("#next_chosen_people_error").fadeIn("slow").fadeOut("slow");
		}
		else
		{
			if (confirm("确定将文件提交给 " + choose_people.find("option:selected").html() + " 继续审批？"))
			{
				$("#doc_next_receiver").val(choose_people.val());
				toggleInput(false);
				$.modal.close();
				postData();
			}
		}
	});
}

/* 显示“选择下一个收件人” */
/*
function showReceiverNext()
{
   getCompanyList("company_list_next", "../xmls/company_position.php", -1);
   $("mengban").show();
   $("doc_receive_next").show();
   chMengban();
}
*/


/* -------------------------------------- */
/* 审批意见 */
/*
function isAgreeOrNot(yn)
{
   if ($F("is_multi_flag") == 0 && $F("db_doc_ismulti") == 0)
	 {
		if (yn == true)
		  {
			 $("opt_1").onclick = goOnApply;
			 $("opt_1").style.color = "black";
		  }
		else
		  {
			 $("opt_1").onclick = function(){void(0);};
			 $("opt_1").style.color = "gray";
		  }
	 }
}
*/

/* ------------------------------------ */
/* ----- 主要操作 ----- */
/* 关闭 */
/*
function closeWindow()
{
   if (confirm("确定取消审批，关闭窗口？"))
	 {
		window.close();
		return false;
	 }
}
*/

/* 继续流转 */
/*
function goOnApply()
{
   submitData(1);
}

/* 结束审批 */
/*
function finishExamin()
{
   submitData(9);
}

/* 继续流转 */
/*
function submitData(flag)
{
   var opinion_v = getRadioValue("opinion[]");
   if (opinion_v == -1)
	 {
		window.alert("您未开审批意见！");
		return false;
	 }
   var opinion = $F("opinion_text");
   switch(opinion_v)
	 {
	  case 1:
		opinion = (opinion.include("请输入意见内容...") || opinion == "") ? "" : opinion;
		break;
	  case 2:
		if (opinion.include("请输入意见内容...") || opinion == "")
		  {
			 window.alert("您的审批意见为“不同意”，请写入不同意的原因。");
			 $("opinion_text").activate();
			 return false;
		  }
		break;
	  case 3:
		if (opinion.include("请输入意见内容...") || opinion == "")
		  {
			 window.alert("您的审批意见为“回退”，请写入回退的原因。");
			 $("opinion_text").activate();
			 return false;
		  }
		break;
	 }

 
   var url = "wf_examin2.php";
   var pars = {
	  "page_status" : 1,
	  "goon_or_finish" : flag,
	  "doc_id" : $F("doc_id"),
	  "handout_id" : $F("doc_receiver_list"),
	  "opn_id" : opinion_v,
		"content" : opinion,
		"exam_stamp" : "",
		"exam_multi_point" : $F("doc_mulpnt"),
	  "is_multi" : $F("is_multi_flag")
   };
   var myAjax = new Ajax.Request(url,{
	  method: "post",
		asynchronous: false,
		parameters: pars,
		onComplete: function(req)
		  {
			 var reqtext = req.responseText;
			 if (reqtext.include("Success"))
			   {
				  // 选择继续流转的审批人，放在最后显示
				  switch(flag)
					{
					 case 1:
					   showReceiverNext();
					   break;
					 case 9:
					   window.close();
					   break;
					}
			   }
			 else 
			   {
				  window.alert(reqtext);
			   }
		  }
   });

}
*/

/* ---------------------------------------------------- */
/* 选择下一个收件人 */
/*
function docReceiverNextYes()
{
   if ($F("doc_receiver_next") < 0)
	 {
		window.alert("您还未选择下一位审批者！");
		return false;
	 }
   $("doc_next_receiver").value = $F("doc_receiver_next");
   
   var url = "wf_examin2.php";
   var pars = {
	  "page_status" : 2,
		"doc_id" : $F("doc_id"),
	  "next_receiver" : $F("doc_receiver_next"),
	  "is_multi" : $F("is_multi_flag")
   };
   var myAjax = new Ajax.Request(url,{
	  asynchronous: false,
		method: "post",
		parameters: pars,
		onComplete: function(req)
		  {
			 var reqText = req.responseText;
			 if (reqText.include("Success"))
			   {
				  window.alert("数据提交成功！");
				  window.close();
			   }
			 else
			   {
				  window.alert(reqText);
			   }
		  }
   });
   
   $("doc_receive_next").hide();
   $("mengban").hide();
}
*/
